package com.yitao.cms.service.order;

import com.google.common.collect.Lists;
import com.skipper.base.data.querydsl.PredicateBuilder;
import com.skipper.base.service.GenericEntityService;
import com.yitao.client.constant.OrderStatusEmnu;
import com.yitao.client.dto.order.MallOrderDetailDto;
import com.yitao.client.dto.order.MallOrderDto;
import com.yitao.client.dto.shopInfo.ShopInfoDto;
import com.yitao.client.model.order.*;
import com.yitao.client.model.product.ProductInfoModel;
import com.yitao.client.model.product.ProductSkuModel;
import com.yitao.client.model.purchase.ProcurementOrderDetailModel;
import com.yitao.cms.constant.BaseEnum;
import com.yitao.cms.service.procurement.CmsProcurementOrderDetailService;
import com.yitao.cms.service.product.ProductCmsService;
import com.yitao.cms.service.product.ProductCmsSkuService;
import com.yitao.cms.service.shopInfo.CmsShopInfoService;
import com.yitao.cms.vo.order.CmsMallOrderDetailVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.naming.ldap.PagedResultsControl;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

@Service
public class CmsOrderDetailService extends GenericEntityService<MallOrderDetailModel, MallOrderDetailDto, QMallOrderDetailModel> {
    public CmsOrderDetailService(EntityManager entityManager) {
        super(entityManager);
    }


    @Autowired
    private ProductCmsService productCmsService;

    @Autowired
    private CmsShopInfoService cmsShopInfoService;

    @Autowired
    private ProductCmsSkuService productCmsSkuService;

    @Autowired
    private CmsProcurementOrderDetailService cmsProcurementOrderDetailService;

    @Autowired
    private CmsOrderService cmsOrderService;

    @Autowired
    private CmsMallOrderRefundService cmsMallOrderRefundService;

    @Autowired
    private CmsMallOrderRefundDetailService cmsMallOrderRefundDetailService;

    /**
     * 根据订单ID查询 订单商品详情信息
     *
     * @param orderId
     * @return
     */
    public List<CmsMallOrderDetailVo> getMallOrderDetailDto(Long orderId) {

        MallOrderModel orderByOrderId = cmsOrderService.getOrderByOrderId(orderId);

        List<CmsMallOrderDetailVo> mallOrderDetailDtoList = Lists.newArrayList();
        List<MallOrderDetailModel> mallOrderDetailModelList = queryFactory.select(Q).from(Q).where(Q.orderId.eq(orderId)).fetch();
        if (mallOrderDetailModelList != null && mallOrderDetailModelList.size() != 0) {
            CmsMallOrderDetailVo mallOrderDetailDto = null;
            for (MallOrderDetailModel mallOrderDetailModel : mallOrderDetailModelList) {
                mallOrderDetailDto = new CmsMallOrderDetailVo();
                BeanUtils.copyProperties(mallOrderDetailModel, mallOrderDetailDto);
                Integer outQuantity = 0;//缺货数量
                //标记缺货
                List<ProcurementOrderDetailModel> procurementOrderDetailInfoByOrderDetailIdAndStockout =
                        cmsProcurementOrderDetailService.getProcurementOrderDetailInfoByOrderDetailIdAndStockout(mallOrderDetailModel.getId());
                if (procurementOrderDetailInfoByOrderDetailIdAndStockout != null && procurementOrderDetailInfoByOrderDetailIdAndStockout.size() != 0) {
                    for (ProcurementOrderDetailModel procurementOrderDetailModel : procurementOrderDetailInfoByOrderDetailIdAndStockout) {
                        outQuantity = outQuantity + (procurementOrderDetailModel.getProductQuantity() - procurementOrderDetailModel.getProcurementQuantity());
                    }
                }
                if (outQuantity > 0) {
                    mallOrderDetailDto.setOutQuantity("缺货 * " + outQuantity);
                }
                if (mallOrderDetailModel.getProductId() != null) {
                    ProductInfoModel productInfoModel = productCmsService.getProductInfoById(mallOrderDetailModel.getProductId());
                    if (productInfoModel != null) {
                        mallOrderDetailDto.setProductImgUrl(productInfoModel.getProductMainImgUrl());
                        mallOrderDetailDto.setProductCode(productInfoModel.getProductCode());
                        mallOrderDetailDto.setProductName(productInfoModel.getProductName());
                        mallOrderDetailDto.setFreightFee(productInfoModel.getFreightFee());
                        if (mallOrderDetailDto.getProductSkuId() != null) {
                            ProductSkuModel productSkuModel = productCmsSkuService.getProductSkuDtoById(mallOrderDetailDto.getProductSkuId());
                            if (productSkuModel != null) {
                                StringBuffer sb = new StringBuffer();
                                /*if (productSkuModel.getAttrSpecName() != null) {
                                    sb.append(productSkuModel.getAttrSpecName());
                                }*/
                                if (productSkuModel.getParentId() != null) {
                                    ProductSkuModel productParentSkuModel = productCmsSkuService.getProductSkuDtoById(productSkuModel.getParentId());
                                    if (productParentSkuModel != null && StringUtils.isNotEmpty(productParentSkuModel.getAttrSpecValue())) {
                                        sb.append(productParentSkuModel.getAttrSpecName()).append(":").append(productParentSkuModel.getAttrSpecValue()).append("  ");
                                    }
                                }
                                if (productSkuModel.getAttrSpecValue() != null) {
                                    /* sb.append("/").*/
                                    sb.append(productSkuModel.getAttrSpecName()).append(":").append(productSkuModel.getAttrSpecValue());
                                }
                                mallOrderDetailDto.setProductSkuName(sb.toString());
                            }
                        }
                    }
                }
                if (mallOrderDetailModel.getShopId() != null) {
                    ShopInfoDto shopInfoDto = cmsShopInfoService.getShopInfoById(mallOrderDetailModel.getShopId());
                    if (shopInfoDto != null) {
                        mallOrderDetailDto.setShopName(shopInfoDto.getShopName());
                    }
                }

                //判断商品是否可以标缺
                Integer isTheLackOf = BaseEnum.YesOrNoEnum.NO.getCode();
                if ((orderByOrderId.getLogisticStatus().equals(OrderStatusEmnu.LOGISTIC_STATUS.WAIT_SHIP.getCode()) ||//待发货
                        orderByOrderId.getLogisticStatus().equals(OrderStatusEmnu.LOGISTIC_STATUS.PORTION_SHIPPED.getCode())) &&//部分发货
                        orderByOrderId.getPayStatus().equals(OrderStatusEmnu.PAY_STATUS.HAVE_PAY.getCode()) &&//已付款
                        ((orderByOrderId.getRefundStatus() == null ||//查询之前的数据
                                orderByOrderId.getRefundStatus().equals(OrderStatusEmnu.REFUND_STATUS.STANDARD_ORDER.getCode()) ||//正常订单
                                orderByOrderId.getRefundStatus().equals(OrderStatusEmnu.REFUND_STATUS.PORTION_APPLYING_FOR_REFUND.getCode()) ||//部分申请退款中
                                orderByOrderId.getRefundStatus().equals(OrderStatusEmnu.REFUND_STATUS.REFUND_REJECTED.getCode()) ||//退款驳回
                                orderByOrderId.getRefundStatus().equals(OrderStatusEmnu.REFUND_STATUS.PORTION_REFUND_OF.getCode()) ||//部分退款中
                                orderByOrderId.getRefundStatus().equals(OrderStatusEmnu.REFUND_STATUS.PORTION_REFUND.getCode()) //||部分已退款
                        ))) {
                    if (mallOrderDetailModel.getIsOutStock() == null || !mallOrderDetailModel.getIsOutStock()) {
                        if (mallOrderDetailModel.getLogisticStatus() == null) {
                            isTheLackOf = BaseEnum.YesOrNoEnum.YES.getCode();
                        }
                    }
                }
                //状态
                String orderStatus = null;

                MallOrderDto mallOrderDto = new MallOrderDto();
                BeanUtils.copyProperties(orderByOrderId, mallOrderDto);
                MallOrderRefundModel afterSaleByOrderCode = cmsMallOrderRefundService.getAfterSaleByOrderCode(mallOrderDto.getOrderCode());
                if (afterSaleByOrderCode == null) {
                    mallOrderDto.setRefundStatus(mallOrderDetailModel.getRefundStatus());
                } else {
                    if (afterSaleByOrderCode.getRefundType().equals(OrderStatusEmnu.REFUND_TYPE.ORDINARY_REFUND.getKey())) {//整单
                        mallOrderDto.setRefundStatus(orderByOrderId.getRefundStatus());
                    } else {
                        mallOrderDto.setRefundStatus(mallOrderDetailModel.getRefundStatus());
                    }
                }
                OrderStatusEmnu.ORDER_SHOW_STATUS order_show_status = buildOrderShowStatusByStatus(mallOrderDto);
                if (orderByOrderId.getLogisticStatus() != null && orderByOrderId.getLogisticStatus().equals(OrderStatusEmnu.LOGISTIC_STATUS.PORTION_SHIPPED.getCode())) {
                    orderStatus = OrderStatusEmnu.LOGISTIC_STATUS.WAIT_SHIP.getMsg();
                }
                if (mallOrderDetailModel.getLogisticStatus() != null && mallOrderDetailModel.getLogisticStatus().equals(OrderStatusEmnu.LOGISTIC_STATUS.SHIPPED.getCode())) {
                    orderStatus = OrderStatusEmnu.LOGISTIC_STATUS.SHIPPED.getMsg();
                }
                if (orderStatus == null) {
                    orderStatus = order_show_status.getValue();
                }
                //判断商品是否可以发货
                Integer isDeliver = BaseEnum.YesOrNoEnum.NO.getCode();
                if ((orderByOrderId.getLogisticStatus().equals(OrderStatusEmnu.LOGISTIC_STATUS.WAIT_SHIP.getCode()) ||//待发货
                        orderByOrderId.getLogisticStatus().equals(OrderStatusEmnu.LOGISTIC_STATUS.PORTION_SHIPPED.getCode())) &&//部分发货
                        orderByOrderId.getPayStatus().equals(OrderStatusEmnu.PAY_STATUS.HAVE_PAY.getCode()) &&//已付款
                        ((orderByOrderId.getRefundStatus() == null ||//查询之前的数据
                                orderByOrderId.getRefundStatus().equals(OrderStatusEmnu.REFUND_STATUS.STANDARD_ORDER.getCode()) ||//正常订单
                                orderByOrderId.getRefundStatus().equals(OrderStatusEmnu.REFUND_STATUS.PORTION_APPLYING_FOR_REFUND.getCode()) ||//部分申请退款中
                                orderByOrderId.getRefundStatus().equals(OrderStatusEmnu.REFUND_STATUS.REFUND_REJECTED.getCode()) ||//退款驳回
                                orderByOrderId.getRefundStatus().equals(OrderStatusEmnu.REFUND_STATUS.PORTION_REFUND_OF.getCode()) ||//部分退款中
                                orderByOrderId.getRefundStatus().equals(OrderStatusEmnu.REFUND_STATUS.PORTION_REFUND.getCode()) //||部分已退款
                        ))) {
                    if (mallOrderDetailModel != null || mallOrderDetailModel.getIsOutStock()) {//缺货的
                        ProcurementOrderDetailModel stockoutInfoByOrderDetailId = cmsProcurementOrderDetailService.getStockoutInfoByOrderDetailId(mallOrderDetailModel.getId());
                        if (stockoutInfoByOrderDetailId != null) {
                            Integer productQuantity = stockoutInfoByOrderDetailId.getProductQuantity();
                            if (productQuantity == null) {
                                productQuantity = 0;
                            }
                            Integer procurementQuantity = stockoutInfoByOrderDetailId.getProcurementQuantity();
                            if (procurementQuantity == null) {
                                procurementQuantity = 0;
                            }
                            Integer stockQuantity = productQuantity - procurementQuantity;
                            if (stockQuantity < mallOrderDetailModel.getQuantity()) {
                                isDeliver = BaseEnum.YesOrNoEnum.YES.getCode();
                                isTheLackOf = BaseEnum.YesOrNoEnum.NO.getCode();
                                orderStatus = "部分缺货";
                            } else {
                                isTheLackOf = BaseEnum.YesOrNoEnum.NO.getCode();
                                orderStatus = "缺货";
                            }
                        } else {
                            if (mallOrderDetailModel.getLogisticStatus() == null) {
                                isDeliver = BaseEnum.YesOrNoEnum.YES.getCode();
                            }
                        }
                    } else {
                        if (mallOrderDetailModel.getLogisticStatus() == null) {
                            isDeliver = BaseEnum.YesOrNoEnum.YES.getCode();
                        }
                    }
                }


                mallOrderDetailDto.setIsDeliver(isDeliver);
                mallOrderDetailDto.setIsTheLackOf(isTheLackOf);
                if (mallOrderDetailModel.getRefundStatus() != null && (mallOrderDetailModel.getIsOutStock() == null || !mallOrderDetailModel.getIsOutStock())) {
                    OrderStatusEmnu.ORDER_SHOW_STATUS orderShowStatus = buildOrderShowStatusByRefundStatus(mallOrderDetailModel.getRefundStatus());
                    orderStatus = orderShowStatus.getValue();
                }
                if (mallOrderDetailModel.getIsOutStock() != null && mallOrderDetailModel.getIsOutStock()) {
                    ProcurementOrderDetailModel stockoutInfoByOrderDetailId = cmsProcurementOrderDetailService.getStockoutInfoByOrderDetailId(mallOrderDetailModel.getId());
                    if (stockoutInfoByOrderDetailId != null) {
                        Integer productQuantity = stockoutInfoByOrderDetailId.getProductQuantity();
                        if (productQuantity == null) {
                            productQuantity = 0;
                        }
                        Integer procurementQuantity = stockoutInfoByOrderDetailId.getProcurementQuantity();
                        if (procurementQuantity == null) {
                            procurementQuantity = 0;
                        }
                        Integer stockQuantity = productQuantity - procurementQuantity;
                        if (stockQuantity < mallOrderDetailModel.getQuantity()) {
                            orderStatus = "部分缺货";
                        } else {
                            orderStatus = "缺货";
                        }
                    }
                }
                if (mallOrderDetailModel.getRefundStatus() != null) {
                    order_show_status = buildOrderShowStatusByStatus(mallOrderDto);
                    orderStatus = order_show_status.getValue();
                }
                mallOrderDetailDto.setOrderStatus(orderStatus);
                mallOrderDetailDtoList.add(mallOrderDetailDto);
            }
        }
        return mallOrderDetailDtoList;
    }


    public MallOrderDetailModel getMallOrderDetailModelById(Long id) {
        return queryFactory.select(Q).from(Q).where(Q.id.eq(id)).fetchOne();
    }

    @Transactional
    public MallOrderDetailModel save(MallOrderDetailModel mallOrderDetailModel) {
        return repository.save(mallOrderDetailModel);
    }

    public List<MallOrderDetailModel> getOrderDetailModelListByOrderId(Long orderId) {
        return queryFactory.select(Q).from(Q).where(Q.orderId.eq(orderId)).fetch();
    }

    public List<MallOrderDetailModel> getOrderDetailModelListNotIn(Long orderId, List<MallOrderDetailModel> mallOrderDetailModelListUpdate) {
        List<Long> idList = Lists.newArrayList();
        for (MallOrderDetailModel mallOrderDetailModel :
                mallOrderDetailModelListUpdate) {
            idList.add(mallOrderDetailModel.getId());
        }
        List<MallOrderDetailModel> fetch = queryFactory.select(Q).from(Q).where(Q.orderId.eq(orderId).and(Q.id.notIn(idList))).fetch();
        if (fetch == null) {
            fetch = Lists.newArrayList();
        }
        return fetch;
    }


    /**
     * 回显枚举值
     *
     * @param mallOrderDto 订单信息
     * @return
     */
    private OrderStatusEmnu.ORDER_SHOW_STATUS buildOrderShowStatusByStatus(MallOrderDto mallOrderDto) {
        Integer orderStatus = mallOrderDto.getOrderStatus();
        Integer payStatus = mallOrderDto.getPayStatus();
        Integer logisticStatus = mallOrderDto.getLogisticStatus();
        Integer orderType = mallOrderDto.getOrderType();
        Integer refundStatus = mallOrderDto.getRefundStatus();
        if (refundStatus == null) {
            refundStatus = OrderStatusEmnu.REFUND_STATUS.STANDARD_ORDER.getCode();
        }
        if (refundStatus == OrderStatusEmnu.REFUND_STATUS.APPLYING_FOR_REFUND.getCode()) {
            return OrderStatusEmnu.ORDER_SHOW_STATUS.APPLY_CANCEL;
        }
        if (refundStatus == OrderStatusEmnu.REFUND_STATUS.PORTION_APPLYING_FOR_REFUND.getCode()) {
            return OrderStatusEmnu.ORDER_SHOW_STATUS.PORTION_APPLYING_FOR_REFUND;
        }
        if (refundStatus == OrderStatusEmnu.REFUND_STATUS.REFUND_REJECTED.getCode()) {
            return OrderStatusEmnu.ORDER_SHOW_STATUS.REFUND_REJECTED;
        }
        if (refundStatus == OrderStatusEmnu.REFUND_STATUS.NOT_REFUND.getCode()) {
            return OrderStatusEmnu.ORDER_SHOW_STATUS.NOT_REFUND;
        }
        if (refundStatus == OrderStatusEmnu.REFUND_STATUS.REFUND.getCode()) {
            return OrderStatusEmnu.ORDER_SHOW_STATUS.REFUND_CANCEL;
        }
        if (refundStatus == OrderStatusEmnu.REFUND_STATUS.PORTION_REFUND.getCode()) {
            return OrderStatusEmnu.ORDER_SHOW_STATUS.PORTION_REFUND;
        }
        if (refundStatus == OrderStatusEmnu.REFUND_STATUS.REFUND_ERROR.getCode()) {
            return OrderStatusEmnu.ORDER_SHOW_STATUS.REFUND_FINISHED;
        }
        if (OrderStatusEmnu.ORDER_STATUS.CANCEL.getCode() == orderStatus.intValue()) {
            return OrderStatusEmnu.ORDER_SHOW_STATUS.CANCELED;
        } else if (OrderStatusEmnu.PAY_STATUS.NOT_PAY.getCode() == payStatus.intValue() && orderStatus != OrderStatusEmnu.ORDER_STATUS.CANCEL.getCode()) {
            //未付款(待付款)
            return OrderStatusEmnu.ORDER_SHOW_STATUS.PENDING;
        } else if (OrderStatusEmnu.PAY_STATUS.HAVE_PAY.getCode() == payStatus.intValue() && orderType != null && orderType.equals(2) && OrderStatusEmnu.ORDER_STATUS.WAIT_GROUP.getCode().equals(orderStatus)) {
            // 已经付款  拼单
            return OrderStatusEmnu.ORDER_SHOW_STATUS.WAIT_GROUP;
        } else if (OrderStatusEmnu.ORDER_STATUS.APPROVAL.getCode() == orderStatus.intValue()
                && (OrderStatusEmnu.LOGISTIC_STATUS.IN_THE_STOCK.getCode() == logisticStatus.intValue())) {
            // 待发货
            return OrderStatusEmnu.ORDER_SHOW_STATUS.WAIT_DELIVER;
        } else if (OrderStatusEmnu.ORDER_STATUS.APPROVAL.getCode() == payStatus.intValue()
                && (OrderStatusEmnu.LOGISTIC_STATUS.WAIT_SHIP.getCode() == logisticStatus.intValue())) {
            // 已捡货
            return OrderStatusEmnu.ORDER_SHOW_STATUS.WAIT_DELIVER;
        } else if (OrderStatusEmnu.LOGISTIC_STATUS.SHIPPED.getCode() == logisticStatus.intValue()) {
            // 已发货
            return OrderStatusEmnu.ORDER_SHOW_STATUS.SHIPPED;
        } else if (OrderStatusEmnu.ORDER_STATUS.FINISH.getCode() == orderStatus.intValue()) {
            // 交易完成
            return OrderStatusEmnu.ORDER_SHOW_STATUS.TRADE_FINISHED;

        } else if (OrderStatusEmnu.ORDER_STATUS.OVER.getCode() == orderStatus.intValue()) {
            // 交易完成
            return OrderStatusEmnu.ORDER_SHOW_STATUS.OVER;
        } else if (OrderStatusEmnu.PAY_STATUS.APPLYING_FOR_REFUND.getCode().equals(payStatus)) {
            return OrderStatusEmnu.ORDER_SHOW_STATUS.APPLY_CANCEL;
        } else if (OrderStatusEmnu.ORDER_SHOW_STATUS.REFUND_REJECTED.getKey().equals(payStatus)) {
            return OrderStatusEmnu.ORDER_SHOW_STATUS.REFUND_REJECTED;
        } else if (OrderStatusEmnu.PAY_STATUS.NOT_REFUND.getCode().equals(payStatus)) {
            return OrderStatusEmnu.ORDER_SHOW_STATUS.REFUND_CANCEL_ING;
        } else if (OrderStatusEmnu.PAY_STATUS.REFUND.getCode().equals(payStatus)) {
            return OrderStatusEmnu.ORDER_SHOW_STATUS.REFUND_CANCEL;
        } else if (OrderStatusEmnu.PAY_STATUS.REFUND_ERROR.getCode().equals(payStatus)) {
            return OrderStatusEmnu.ORDER_SHOW_STATUS.REFUND_FINISHED;
        } else if (OrderStatusEmnu.LOGISTIC_STATUS.PORTION_SHIPPED.getCode().equals(logisticStatus)) {
            return OrderStatusEmnu.ORDER_SHOW_STATUS.PARTIAL_DELIVER;
        } else {
            //订单异常
            return OrderStatusEmnu.ORDER_SHOW_STATUS.ERROR_ORDER;
        }
    }


    private OrderStatusEmnu.ORDER_SHOW_STATUS buildOrderShowStatusByRefundStatus(Integer refundStatus) {
        if (refundStatus == OrderStatusEmnu.REFUND_STATUS.APPLYING_FOR_REFUND.getCode()) {
            return OrderStatusEmnu.ORDER_SHOW_STATUS.APPLY_CANCEL;
        }
        if (refundStatus == OrderStatusEmnu.REFUND_STATUS.REFUND_REJECTED.getCode()) {
            return OrderStatusEmnu.ORDER_SHOW_STATUS.REFUND_REJECTED;
        }
        if (refundStatus == OrderStatusEmnu.REFUND_STATUS.NOT_REFUND.getCode()) {
            return OrderStatusEmnu.ORDER_SHOW_STATUS.NOT_REFUND;
        }
        if (refundStatus == OrderStatusEmnu.REFUND_STATUS.PORTION_REFUND_OF.getCode()) {
            return OrderStatusEmnu.ORDER_SHOW_STATUS.NOT_REFUND;
        }
        if (refundStatus == OrderStatusEmnu.REFUND_STATUS.REFUND.getCode()) {
            return OrderStatusEmnu.ORDER_SHOW_STATUS.REFUND_CANCEL;
        }
        if (refundStatus == OrderStatusEmnu.REFUND_STATUS.PORTION_REFUND.getCode()) {
            return OrderStatusEmnu.ORDER_SHOW_STATUS.REFUND;
        }
        if (refundStatus == OrderStatusEmnu.REFUND_STATUS.REFUND_ERROR.getCode()) {
            return OrderStatusEmnu.ORDER_SHOW_STATUS.REFUND_FINISHED;
        }
        if (refundStatus == OrderStatusEmnu.REFUND_STATUS.PORTION_APPLYING_FOR_REFUND.getCode()) {
            return OrderStatusEmnu.ORDER_SHOW_STATUS.APPLYING_FOR_REFUND;
        }
        return OrderStatusEmnu.ORDER_SHOW_STATUS.ERROR_ORDER;
    }

    public MallOrderDetailModel getMallOrderDetailModelByOrderIdAndSkuId(Long orderId, Long skuId) {
        return queryFactory.select(Q).from(Q).where(Q.orderId.eq(orderId).and(Q.productSkuId.eq(skuId))).fetchOne();
    }


    public List<MallOrderDetailModel> getChildItemTotalAmount(Long orderId, Long orderDetailId) {
        return queryFactory.select(Q).from(Q).where(Q.orderId.eq(orderId).and(Q.id.notIn(orderDetailId))).fetch();
    }

    @Transactional
    public int updateRefundStatus(Long orderId, Integer refundStatus) {
        int i = 0;
        MallOrderRefundModel orderRefundByOrderId = cmsMallOrderRefundService.findOrderRefundByOrderId(orderId);
        if (orderRefundByOrderId != null) {
            List<MallOrderRefundDetailModel> byRefundId = cmsMallOrderRefundDetailService.findByRefundId(orderRefundByOrderId.getId());
            if (!CollectionUtils.isEmpty(byRefundId)) {
                List<Long> list = new ArrayList<>();
                for (MallOrderRefundDetailModel m : byRefundId) {
                    list.add(m.getOrderDetailId());
                }
                if (!CollectionUtils.isEmpty(list)) {
                    i = repository.updateByWhere(PredicateBuilder.conjunction()
                            .and(() -> Q.id.in(list))
                            .get(), it -> it.set(Q.refundStatus, refundStatus));
                }
            }
        }
        return i;
    }
}
